package com.example.oldguy.modules.app.controllers;

import com.example.oldguy.common.dto.CommonRsp;
import com.example.oldguy.modules.app.dto.rsp.TaskRsp;
import com.example.oldguy.modules.app.services.TaskInstanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: TaskController
 * @Author: huangrenhao
 * @Description:
 * @CreateTime： 2020/2/11 0011 下午 9:13
 * @Version：
 **/
@Api(tags = "工作流流转相关")
@RestController
@RequestMapping("/workFlow")
@Slf4j
public class WokrFlowController {

    @Autowired
    private TaskInstanceService taskInstanceService;

    @Autowired
    private TaskService taskService;


    @Autowired
    private RuntimeService runtimeService;

    @ApiOperation("发起任务")
    @PostMapping("/startWorkFlow")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "modelKey", value = "模版KEY", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "zc", value = "是否暂存默认false不暂存", required = true, dataType = "boolean"),
            @ApiImplicitParam(paramType = "query", name = "map", value = "流转所需参数", dataType = "Map")
    })
    public CommonRsp completeTask(String modelKey,boolean zc ,@RequestParam(required = false) Map<String,Object> map) {
        CommonRsp commonRsp = new CommonRsp();
        try {
            ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(modelKey, map);
            if(!zc){
                List<TaskRsp> rspList= taskInstanceService.getTaskListByProcessInstanceId(processInstance.getId());
                this.approvalWorkFlow(rspList.get(0).getTaskId(),map);
            }
            commonRsp.setData(processInstance.getId());
            return commonRsp;
        } catch (Exception e) {
            log.error("发起工作流异常", e);
            commonRsp.setStatus(1);
            commonRsp.setData(e);
        }
        return commonRsp;
    }

    /**
     * 审批
     *
     * @param taskId 任务ID
     */
    @ApiOperation("审批通过")
    @PostMapping(value = "/approvalWorkFlow")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "taskId", value = "任务ID", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "map", value = "流转所需参数", dataType = "Map")
    })
    public CommonRsp approvalWorkFlow(String taskId, @RequestParam(required = false) Map<String,Object> map) {
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        if (task == null) {
            return new CommonRsp<>(1, null, "任务不存在");
        }
        try {
            taskService.complete(taskId, map);
        } catch (Exception e) {
            log.error("提交工作流异常", e);
            return new CommonRsp<>(1, e, "提交工作流异常");
        }
        return CommonRsp.SUCCESS;
    }


}
